package com.pucong.gitee.database.backup.business.sys.controller;

import com.pucong.gitee.database.backup.business.sys.service.SysRoleService;
import com.pucong.gitee.database.backup.common.entity.sys.SysRole;
import com.pucong.gitee.database.backup.common.entity.sys.SysRoleResource;
import com.pucong.gitee.database.backup.common.entity.sys.dto.SysRoleSaveDto;
import com.pucong.gitee.database.backup.common.entity.sys.vo.AddSysRoleResourceDto;
import com.pucong.gitee.database.backup.utils.Res;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author pucong
 * @company gitee.com/pucong
 * @create 2020-12-01
 */
@RestController
@RequestMapping("/sys/roleInfo")
@Api(value = "用户基础信息-API ", tags = "02. 用户管理")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @GetMapping("/getSysRoleInfoList")
    @ApiOperation(value = "查询当前用户的角色")
    public Res<List<SysRole>> getSysRoleInfoList() {
        List<SysRole> sysRoleInfoList = sysRoleService.getSysRoleInfoList();
        return Res.ok(sysRoleInfoList);
    }

    @PostMapping("/insertSysRoleInfo")
    @ApiOperation(value = "02新增")
    public Res<SysRole> insertSysRoleInfo(@Valid @RequestBody SysRoleSaveDto dto) {
        SysRole sysRole = sysRoleService.insertSysRoleInfo(dto);
        return Res.ok(sysRole);
    }

    @PutMapping("/updateSysRoleInfoById/{id}")
    @ApiOperation(value = "03根据用户id更新数据")
    public Res updateSysRoleInfoById(@ApiParam("主键") @PathVariable("id") String id, @Valid @RequestBody SysRoleSaveDto dto) {
        sysRoleService.updateSysRoleInfoById(id, dto);
        return Res.ok();
    }

    @DeleteMapping("/deleteSysRoleInfoById/{id}")
    @ApiOperation(value = "04根据主键id删除数据")
    public Res deleteSysRoleInfoById(@ApiParam("主键") @PathVariable("id") String id) {
        sysRoleService.deleteSysRoleInfoById(id);
        return Res.ok();
    }

    @GetMapping("/getRoleResourceByRoleId/{id}")
    @ApiOperation(value = "根据role id获取资源id")
    public Res<List<String>> getRoleResourceByRoleId(@ApiParam("主键") @PathVariable("id") String id) {
        List<String> ids = sysRoleService.getRoleResourceByRoleId(id);
        return Res.ok(ids);
    }

    @PostMapping("/addSysRoleResource/{id}")
    @ApiOperation(value = "05新增角色关联资源")
    public Res<SysRoleResource> addSysRoleResource(@ApiParam("角色主键") @PathVariable("id") String id, @Valid @RequestBody AddSysRoleResourceDto addSysRoleResourceDto) {
        SysRoleResource sysRoleResource = sysRoleService.addSysRoleResource(id, addSysRoleResourceDto);
        return Res.ok(sysRoleResource);
    }

    @PostMapping("/deleteSysRoleResource/{roleId}/{resourceId}")
    @ApiOperation(value = "06删除角色关联资源")
    public Res deleteSysRoleResource(@ApiParam("角色主键") @PathVariable("roleId") String roleId,@ApiParam("资源主键") @PathVariable("resourceId") String resourceId) {
        sysRoleService.deleteSysRoleResource(roleId,resourceId);
        return Res.ok();
    }
}
